//
//  ViewController.m
//  题1
//
//  Created by iOS001 on 15/12/12.
//  Copyright © 2015年 iOS001. All rights reserved.
//

#import "FDTableVIewVC.h"
#import "FDCellModel.h"
#import "FDTableViewCellView.h"
#import "FDBaseViewVC.h"
#import "MJExtension.h"


@interface FDTableVIewVC ()<UITableViewDataSource,UITableViewDelegate>

//模型数组
@property (nonatomic,strong) NSArray *models;

@end

@implementation FDTableVIewVC


#pragma mark- 懒加载
-(NSArray *)models{
    
    if (_models == nil) {
        
        NSString *path=[[NSBundle mainBundle]pathForResource:@"complica.plist" ofType:nil];
        NSArray *array=[[NSArray alloc]initWithContentsOfFile:path];
        
        
        //取出本地的字典数组
        NSString * docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) lastObject];
        NSString * fileName = [docDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@labelStr3",self.title]];
        NSArray * models = [NSArray arrayWithContentsOfFile:fileName];
        //将取出来的字典数组转化为模型数组
        NSMutableArray *arrayDict=[NSMutableArray array];
        for (int i=0; i<models.count; i++) {
            
            NSDictionary *dict=models[i];
            //字典转模型
            FDCellModel *cellModel=[FDCellModel modelWithDict:dict];
            [arrayDict addObject:cellModel];
        }
        //判断当有本地数据的时候加载本地数据
        if (arrayDict.count) {
            
            _models=arrayDict;
            
        }else{
            
            NSMutableArray *temArray=[NSMutableArray array];
            
            [array enumerateObjectsUsingBlock:^(NSDictionary *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                
                FDCellModel *cellModel=[FDCellModel modelWithDict:obj];
                [temArray addObject:cellModel];
            }];
            
            _models = temArray;
        }
        
        
    }
    return _models;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //1.创建tableView
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    tableView.delegate = self;
    tableView.dataSource = self;
    [self.view addSubview:tableView];
    
    UIBarButtonItem *item=[[UIBarButtonItem alloc]initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(clickDone)];
    
    self.navigationItem.rightBarButtonItem=item;
}

-(void)cellback:(CallBackBlock)callBackBlok{
    
    self.callBackBlok=callBackBlok;
}

-(void)clickDone{
    
    NSMutableArray *array=[NSMutableArray array];
    [self.models enumerateObjectsUsingBlock:^(FDCellModel *  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        if (obj.isClick) {
            
            [array addObject:obj];
        }
    }];
    
    if (array.count==0) {
        
        NSString *path=[[NSBundle mainBundle]pathForResource:@"complica.plist" ofType:nil];
        NSArray *array=[[NSArray alloc]initWithContentsOfFile:path];
        
        //保存数组在本地
        NSString * docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) lastObject];
        
        NSString * fileName = [docDir stringByAppendingPathComponent:@"models"];
        
        [array writeToFile:fileName atomically:YES];
    }
    if (self.callBackBlok) {
        
        self.callBackBlok(self.models);
    }
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - tableView数据源
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.models.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *ID = @"cell";
    
    FDTableViewCellView *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        
        cell = [[FDTableViewCellView alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    FDCellModel *cellModel=self.models[indexPath.row];
    
    cell.textLabel.text=[NSString stringWithFormat:@"%@",cellModel.title];
    
    //给cell的模型属性赋值
    cell.model = cellModel;
    
    return cell;
}

#pragma mark - tableView代理方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //取出模型
    FDCellModel *model = self.models[indexPath.row];
    
    //修改模型数据
    model.isClick = !model.isClick;
    
    //刷新当前行
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
    
    
}







@end
